
const GameStatus = require("GameStatus");
const SoundType = require("SoundType");
const MainControl = require("MainControl");


cc.Class({
    extends: cc.Component,
    properties: {
        
        speed:0,
        canvas:{
            default:null,
            type:cc.Canvas
        },

        mainControl:{
            default:null,
            type:MainControl,
        }
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        this.canvas.node.on(cc.Node.EventType.TOUCH_START,this.onTouchStart,this);
        this.mainControl = this.canvas.getComponent('MainControl');
    },

    start () {

    },

    update (dt) {

        console.log("update gameStatus ->",this.mainControl.gameStatus);
        if(this.mainControl.gameStatus != GameStatus.Game_Playing)
            return;

        this.speed -= 0.05;
        this.node.y += this.speed;
        let angle = -(this.speed/2) * 30;
        if(angle > 30)
            angle = 30;
        this.node.angle = -angle;
        // this.node.r

        if(this.node.y >=256 ||this.node.y <= -256){
            this.mainControl.gameOver();
            this.speed = 0;
        }
    },

    onTouchStart(event){
        console.log("onTouch ->",this.mainControl.gameStatus);
        if(this.mainControl.gameStatus != GameStatus.Game_Playing)
            return;
        this.speed = 2;
        this.mainControl.audioControl.playSound(SoundType.E_SOUND_FLY);
    },

    onCollisionEnter(other,self){

        cc.log('GameOver');
        this.mainControl.gameOver();
        this.speed = 0;
    }
});
